﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Entities;
using ExpertMIS.Brokers;
using System.Collections;
using System.Text;

namespace ExpertMIS.Forums.UserControl
{
    public partial class ucUserCard : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void LoadExpertDomains(ExpertBroker expertBroker,ExpertEntity expert)
        {
            StringBuilder sb = new StringBuilder();
            
            foreach (DomainEntity domain in expertBroker.GetExpertDomains(expert.OID))
            {
                sb.Append(string.Format("【<a href='DomainIntroduce.aspx?oid={0}' alt='{2}' target='_blank'>{1}</a>】", domain.OID, domain.DomainName, domain.Remark));
            }

            lblExpertDomains.Text = sb.ToString();
            trExpertDomains.Visible = true;
        }

        public string CardClass
        {
            get;
            set;
        }

        public void LoadAsUser(string UserOID)
        {
            Hashtable htKeys = new Hashtable();
            htKeys["OID"] = UserOID;
            ADMUserBroker userBroker = new ADMUserBroker();
            ADMUserEntity user = userBroker.GetEntity(htKeys) as ADMUserEntity;

            if (user == null)
            {
                lblUserName.Text = "<span style='font-size:9pt;color:#595959'>用户已被删除</span>";
                return;
            }

            if (user.IsExpert.Value == true)
            {
                ExpertBroker broker = new ExpertBroker();
                ExpertEntity entity = (broker.GetExpertEntityByUserID(user.OID)) as ExpertEntity;

                if (entity != null)
                {
                    imgPoster.ImageUrl = "~/Service/GetPhoto.aspx?w=128&h=128&filename=" + entity.Graphic;
                    lblUserName.Text = entity.Name;
                    LoadExpertDomains(broker, entity);
                }
            }
            else
            {
                imgPoster.ImageUrl = "~/Service/GetPhoto.aspx?w=128&h=128&filename=" + user.Graphic;
                lblUserName.Text = user.Name;
            }
        }

        public void LoadAsExpert(string ExpertOID)
        {
            Hashtable htKeys = new Hashtable();
            htKeys["OID"] = ExpertOID;

            ExpertBroker broker = new ExpertBroker();
            ExpertEntity entity = broker.GetEntity(htKeys) as ExpertEntity;

            if (entity != null)
            {
                imgPoster.ImageUrl = "~/Service/GetPhoto.aspx?w=128&h=128&filename=" + entity.Graphic;
                lblUserName.Text = entity.Name;
                LoadExpertDomains(broker, entity);
            }
        }
    }
}